﻿import Request from "../../base/request.js?t=202511041006"
import Storage from "../../base/storage.js?t=202511041006"
import { getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
 
    const data = reactive({
        pdfOpen: false,
        pdfurl: "",

        messageTest: "",
        proxy: "",
        queryParams: {
   
            JobTypeId: null
        },
        templateOpen:false,
        typeList: [
        
        ],
        tabList:[],
        opType:0,
        editInfo: {
            type: null,
            tableInfo: [
                {
                    name: null,
                   
                    day: null
                }
             

            ],
            options: [],
            options1: [],
            options2: [ ]

        },
        pagination: {
            total: 0,
            current: 1,
            loading: false,
            pageSize: 10,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗

        },

  

        columns: [
            {
                title: '序号',
                dataIndex: 'no',
                key: 'no',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1                },
            }, 
            {
                title: '工种',
                dataIndex: 'JobType',
                key: 'JobType',
                align: 'center'

            },
            {
                title: '劳防用品',
                dataIndex: 'PPEList',
                key: 'PPEList',
                align: 'center',
                customRender: ({ text }) => {
    
                    return text.map(e => e.Name).toString()
                },
            },
   

            {
                title: '操作',
                key: 'action',
                align: 'center',
                fixed: 'right',
                width: 100,
                customRender: function ({ text, record, index, column }) {

       

                    const delBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link"
                    }, { default: () => '删除' })

                    const fDelBtn = h(antd.Popconfirm, {
                        title: "确定删除此数据？",
                        onConfirm: () => {
                            methods.DeleteJobPPEItem(record)
                        }

                    }, { default: () => delBtn })


                    const modifyBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        onClick: () => {
                            methods.handleEdit(text)
                        }
                    }, { default: () => '编辑' })




                    const detailBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        onClick: () => {
                            methods.handleDetail(text)
                        }
                    }, { default: () => '详情' })



                    //fDelBtn

                    return h(antd.Space, {
                    },
                        () => [modifyBtn, detailBtn]
                    );

                }
            },
        ],
        dataSource: [],
    })
    let params = toRefs(data)
    const methods = {
        filterOption: (val, type) => {
            return `${type.value}`.includes(`${val || ''}`)
        },
        clearConfig: () => {
            params.editInfo.value.tableInfo=[{

                name: null,
                type: null,
                day: null

            }]
        },
        /// PPE / GetJobTypeList

        handleQuery: () => {

            methods.getTableList(true)

        },
        handleTypeChange: async (e, i) => {
            params.editInfo.value.tableInfo[i].name = null
            let res = await methods.GetBasicList(e)
            params.editInfo.value.tableInfo[i].lfoptions=res
        },

    
        DeleteJobPPEItem: (record) => {
        Request({
            url: "/PPE/DeleteJobPPEItem",
            method: 'post',
            data: {
                Id: record.Id
            }
        }).then(res => {
            params.messageTest.value.info(res.Msg);
            methods.getTableList(true)
        })
    },
    GetJobTypeList: () => {
        Request({
            url: "/PPE/GetJobTypeList",
            method: 'post',
   
        }).then(res => {

            params.typeList.value = res.Data.map(e => {
                return {
                    label: e.JobType,
                    value:e.Id

                }
            })
            params. editInfo.value.options = res.Data.map(e => {
                return {
                    label: e.JobType,
                    value: e.Id

                }
            })
        })
    },

        addConfig: () => {
            params.editInfo.value.tableInfo.push({
                   
                    name: null,
                type: null,
                day: null
                
            })
        },
        delConfig: (item,i) => {

            

            Request({
                url: "/PPE/DeleteJobPPEItem",
                method: 'post',
                data: {
                    Id: item.Id,
                   
                }
            }).then(res => {
                methods.getTableList(true)
                params.messageTest.value.info(res.Msg);
                params.editInfo.value.tableInfo.splice(i, 1)

            })


        
        },
        cday: (item, index) => {
            console.log(item,'item')
            Request({
                url: "/PPE/UpdateJobPPEItem",
                method: 'post',
                data: {
                    Id: item.Id,
                    Interval: params.editInfo.value.tableInfo[index].day
                }
            }).then(res => {
                methods.getTableList(true)
                params.messageTest.value.info(res.Msg);

            })

        },
        handleEdit: (record) => {
            params.opType.value=1
            params.editInfo.value.type = record.Id
            params.editInfo.value.tableInfo = record.PPEList.map(e => {
                return {
                    name: e.BasicId,
                    Id: e.Id,
                    day: e.Interval,
                    ...e
                }
            })
            console.log(params.editInfo.value.tableInfo,'params.editInfo.value.tableInfo')
            params.templateOpen.value=true
        },
   

        handleDetail: (record) => {
            params.opType.value = 2
            params.editInfo.value.type = record.Id
            params.editInfo.value.tableInfo = record.PPEList.map(e => {
                return {
                    name: e.BasicId,
                    Id: e.Id,
                    day: e.Interval
                }
            })
            params.templateOpen.value = true
        },

        GetBasicList: (id) => {
            return new Promise((resolve,rej) => {
                Request({
                    url: "/PPE/GetBasicList",
                    method: 'post',
                    data: {
                        TypeId: id
                    }
                }).then(res => {
                let fers= res.Data.map(e => {
                        return {
                            label: e.ProductName,
                            value: e.Id
                        }
                    })
                    resolve(fers)


                }).catch(err => {
                    rej(err)
                })

            })
      
        },
        GetEuqTypeList: () => {
            Request({
                url: "/PPE/GetEuqTypeList",
                method: 'post',

            }).then(res => {

                params.tabList.value = res.Data.map(e => {
                    return {
                        label: e.TypeName,
                        value: e.Id
                    }
                })

            })
        },
        subProConfig: () => {

            if (params.opType.value==0) {
                Request({
                    url: "/PPE/AddJobPPEList",
                    method: 'post',
                    data: params.editInfo.value.tableInfo.map(e => {

                        return {
                            BasicId: e.name,
                            JobTypeId: params.editInfo.value.type,
                            Interval: e.day
                        }
                    }),
                }).then(res => {
                    params.messageTest.value.info(res.Msg);
                    methods.getTableList(true)
                    params.templateOpen.value = false
                })
            } else {
                params.templateOpen.value = false
            }

        

        },


        addProConfig: () => {
            params.opType.value=0
            params.editInfo.value.tableInfo = [{
                name: null,
                type: null,
                day: null
            }]
            params.templateOpen.value=true 
        },
        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            Request({
                url: "/PPE/GetJobPPEPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize,
                    ...params.queryParams.value
                },
            }).then(res => {
    
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data.TotalItemCount
                params.pagination.value.loading = false
            })
        },

        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

          


            params.pagination.value.loading = true

            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)
                methods.GetJobTypeList()
                methods.GetEuqTypeList()

            })
        },
   
    }
    const template = {

        pdfPopModel: {
            template: `
           <a-modal v-model:open="pdfOpen" width = "80%" title = "PDF预览" v-on:ok="pdfOpen=false" >
<div style='height:700px'>
                <embed id="pdfviewscreen" :src="pdfurl" type="application/pdf" width="100%" style="height:100%;" />

</div>
  
            </a-modal >
`,
            setup() {
                return {
                    ...params,
                    ...methods,
                };
            }
        },
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}
export default Composition